package com.jprocms.module.cms.controller.portal.page;

import com.jprocms.module.cms.dal.dataobject.site.SiteDO;
import com.jprocms.module.cms.util.CmsContextUtil;
import com.jprocms.module.cms.util.FrontContextUtil;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

import static com.jprocms.module.cms.enums.ErrorCodePortalConstants.TPL_COMMON_ERROR_TPL;


@Controller
public class CustomErrorController implements ErrorController {
    private static final String PAGE_NOT_FOUND = "sys/404";

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request) {
        // 可以添加自己的逻辑，例如根据不同的错误代码返回不同的视图
        // 返回404错误页面，假设你有一个error/404.html模板
        SiteDO site = CmsContextUtil.getSite();
        FrontContextUtil.setData(request);
        return site.joinTemplate(PAGE_NOT_FOUND);
    }

    @GetMapping("/error/siteClose")
    public String handleSiteCloseError(HttpServletRequest request) {
        SiteDO site = CmsContextUtil.getSite();
        FrontContextUtil.setData(request);
        return site.joinTemplate(TPL_COMMON_ERROR_TPL);
    }


}